/*
 * Tema.java
 *
 * Created on 15 de abril de 2007, 18:11
 */

package Modelo;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Pellizard
 */
public class Chapter {
    
    private int order;
    private String name;
    private Index index;
    
    /** Creates a new instance of Tema */
    public Chapter( int orden, String nombre, Index indice ) {
        
        this.order = orden;
        this.name = nombre;
        this.index = indice;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int orden) {
        this.order = orden;
    }

    public String getName() {
        return name;
    }

    public void setName(String nombre) {
        this.name = nombre;
    }

    public Index getIndex() {
        return index;
    }

    public void setIndex(Index indice) {
        this.index = indice;
    }
    
    public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();

          return toXml( doc );
      }
      catch( Exception ex )
      {
         return null;
      }
  }
    
    public Node toXml( Document doc ){
        
      try {
      Node root = doc.createElement("chapter");
      //doc.appendChild(root);

      Node orden = doc.createElement("order");
      orden.appendChild(doc.createTextNode("" + this.order));
      root.appendChild(orden);
      
      Node nombre = doc.createElement("name");
      nombre.appendChild(doc.createTextNode(this.name));
      root.appendChild(nombre);
      
      root.appendChild( index.toXml(doc) );
      
      return root;

    } catch (Exception ex) {
      System.err.println("+============================+");
      System.err.println("|        XML Error           |");
      System.err.println("+============================+");
      System.err.println(ex.getClass());
      System.err.println(ex.getMessage());
      System.err.println("+============================+");
      ex.printStackTrace();
      return null;
    }
  } 
    
    public static Chapter fromXml( String str )
  {
        String name = "";
        int order = 0;
        Index index = new Index();
      
        ByteArrayInputStream is;
        try{
             is = new ByteArrayInputStream( str.getBytes("UTF8") );
        }catch(Exception e){
            return null;
        }
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            return fromXml( doc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }
    
    public static Chapter fromXml( Document doc )
  {
        String name = "";
        int order = 0;
        Index index = new Index();
      
        try
        {
            NodeList nodes = doc.getElementsByTagName( "order" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                order = Integer.parseInt( node.getFirstChild().getNodeValue() );
            }
            
            nodes = doc.getElementsByTagName( "name" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                name = node.getFirstChild().getNodeValue();
            }
            
            nodes = doc.getElementsByTagName( "index" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                index = Index.fromXml( Util.node2String( node ) );
            }
            
            return new Chapter( order, name, index );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }

}
